package views;

import DAO.ICcardDao;
import DAO.vipTabDao;
import Entity.ICcard;
import Entity.Manger;
import Entity.vipTab;
import util.Get_Now_Time;
import util.NotNull;
import util.Result;
import util.RfidTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;

public class MemberRegistration {
    JLabel l1;
    JLabel l2;
    JLabel l3;
    JTextField txt3;
    JLabel l4;
    JTextField txt4;
    JLabel l5;
    JLabel l6;
    JTextField txt6;
    JTextField txtrest;
    JTextField txtin;
    JButton btnOK;
    JButton btn3;
    JComboBox sex;   //下拉框
    JButton card_read;
    vipTabDao vipTabDao=new vipTabDao();
    ICcardDao iCcardDao=new ICcardDao();
    vipTab vipTab=new vipTab();


    class PanelBg extends  JPanel{
        Dimension d;
        Image image;
        PanelBg(Dimension d,Image image){
            this.d=d;
            this.image=image;
        }
        @Override
        public void paintComponent(Graphics g){
            g.drawImage(image,0,0,d.width,d.height,this);
        }
    }
    Dimension fmsize=new Dimension(600,700);
    ImageIcon imageicon = new ImageIcon(this.getClass().getResource("/erhai4.jpg"));
    MemberRegistration(Result result){
        JFrame jframe = new JFrame();
        jframe.setSize(600,700);
        jframe.setDefaultCloseOperation(jframe.DISPOSE_ON_CLOSE);
        jframe.setLayout(null);
        jframe.setLocationRelativeTo(null);


        PanelBg pContent = new PanelBg(fmsize,imageicon.getImage());
        pContent.setLayout(null);
        pContent.setBounds(0,0,600,700);
        jframe.getContentPane().add(pContent);



        l1 = new JLabel("姓名");
        l1.setBounds(150,100,50,30);
       l1.setForeground(Color.white);
        pContent.add(l1);

        txtrest = new JTextField();
        txtrest.setBounds(200,100,200,30);
        txtrest.setOpaque(false);
        txtrest.setForeground(Color.white);
        pContent.add(txtrest);


        l2 = new JLabel("电话");
        l2.setBounds(150,150,50,30);
        l2.setForeground(Color.white);
        pContent.add(l2);

        txtin = new JTextField();
        txtin.setBounds(200,150,200,30);
        txtin.setOpaque(false);
        txtin.setForeground(Color.white);
        pContent.add(txtin);

        l3 = new JLabel("身份证");
        l3.setBounds(150,200,50,30);
        l3.setForeground(Color.white);
        pContent.add(l3);

        txt3 = new JTextField();
        txt3.setBounds(200,200,200,30);
        txt3.setOpaque(false);
        txt3.setForeground(Color.white);
        pContent.add(txt3);

        l4 = new JLabel("住址");
        l4.setBounds(150,250,50,30);
        l4.setForeground(Color.white);
        pContent.add(l4);

        txt4 = new JTextField();
        txt4.setBounds(200,250,200,30);
        txt4.setOpaque(false);
        txt4.setForeground(Color.white);
        pContent.add(txt4);


        l5 = new JLabel("性别");
        l5.setBounds(150,300,50,30);
        l5.setForeground(Color.white);
        pContent.add(l5);

        sex=new JComboBox();
        sex.addItem("--请选择--");    //向下拉列表中添加一项
        sex.addItem("男");
        sex.addItem("女");
        sex.setBounds(200,300,100,30);
        pContent.add(sex);


        l6 = new JLabel("卡号");
        l6.setBounds(150,350,50,30);
        l6.setForeground(Color.white);
        pContent.add(l6);

        txt6 = new JTextField();
        txt6.setBounds(200,350,200,30);
        txt6.setOpaque(false);
        txt6.setForeground(Color.white);
        pContent.add(txt6);

        card_read=new JButton("读卡");
        card_read.setBounds(430,350,70,30);
        card_read.setBackground(Color.blue);
        card_read.setForeground(Color.white);
        card_read.setOpaque(false);
        pContent.add(card_read);
        card_read.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RfidTool instance = RfidTool.getInstance();
                String cardID = instance.showCardId();//获取id卡的号码
                txt6.setText(cardID);
            }
        });



        //初始化时判断是新增还是修改

        if(result.getStatus().equals("0")){  //暂定0为修改
            String icId=""+result.getMessage();
            vipTab= vipTabDao.get_vip_byID(icId);
            ICcard iCcard=iCcardDao.get_byID(icId);
            txtrest.setText(vipTab.getName());
            txtin.setText(vipTab.getTel());
            txt3.setText(vipTab.getIdentity());
            txt4.setText(vipTab.getAddress());
            txt6.setText(iCcard.getIccard());
        }




        btn3 =new JButton("保存");
        btn3.setBounds(100,500,70,30);
        btn3.setBackground(Color.blue);
        btn3.setOpaque(false);
        btn3.setForeground(Color.white);
        pContent.add(btn3);
        btn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!new NotNull().IsNull(txt3,txt4,txt6)){
                    JOptionPane.showMessageDialog(null,"不能为空");
                    return;
                }

                if(result.getStatus().equals("0")){  //暂定1为修改
                    vipTab.setName(txtrest.getText().trim());
                    vipTab.setTel(txtin.getText().trim());
                    vipTab.setIdentity(txt3.getText().trim());
                    vipTab.setAddress(txt4.getText().trim());
                    vipTab.setSex(sex.getSelectedItem().toString());
                    ICcard iCcard=new ICcard();
                    iCcard.setIccard(txt6.getText().trim());
                    iCcard.setIcID(vipTab.IcID);
                    iCcard.setIcstatu(2);
                    iCcard.setIcDate(new Get_Now_Time().Getnow());
                    iCcard.setCardCreat("");
                    iCcard.setIDRest("0");
                    System.out.println("---------------");
                    vipTabDao.update3(vipTab);
                    System.out.println("------------");
                    iCcardDao.update3(iCcard);



                }
                else if(result.getStatus().equals("1")) {  //暂定为新增
                        vipTab vip=new vipTab();
                        ICcard iCcard=new ICcard();
                        vip.setName(txtrest.getText().trim());
                        vip.setTel(txtin.getText().trim());
                        vip.setIdentity(txt3.getText().trim());
                        vip.setAddress(txt4.getText().trim());
                        vip.setSex(sex.getSelectedItem().toString());
                        iCcard.setIccard (txt6.getText().trim());
                        //通过实体卡号获取卡编号存入数据库

                    try {
                        iCcard.setIcID(iCcardDao.getIcID(iCcard.getIccard()));
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    iCcard.setIcstatu(2);
                    iCcard.setIcDate(new Get_Now_Time().Getnow());
                    iCcard.setIDRest("0");
                    iCcard.setCardCreat("");
                    vip.setIcID(iCcard.getIcID());     //获取到的ICID存入VIP中
                    vipTabDao.insert(vip);             //插入数据库VIP表中
                    //开完卡后更改卡的状态
                    iCcardDao.update3(iCcard);
                }
                JOptionPane.showMessageDialog(null,"保存成功");
            }
        });

        btnOK = new JButton("重置");
        btnOK.setBounds(400,500,70,30);
        btnOK.setForeground(Color.white);
        btnOK.setBackground(Color.blue);
        btnOK.setOpaque(false);
        pContent.add(btnOK);
        btnOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txtrest.setText("");
                txtin.setText("");
                txt3.setText("");
                txt4.setText("");
                txt6.setText("");
            }
        });

        jframe.setVisible(true);

    }

//    public static void main(String[] args) {
//        Result result=new Result();
//        result.setStatus("0");
//        String icid="13231908139";
//        result.setMessage(icid);
//        MemberRegistration memberRegistration=new MemberRegistration(result);
//    }
}
